package cn.itsource.org.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.save(department);
        this.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        String dirPath = "";
        if (department.getParent() == null){
            dirPath = "/" +department.getId();
        }else {
            Department parent = departmentMapper.loadById(department.getParent().getId());
            dirPath = parent.getDirPath()+"/"+department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }
    /*查询部门树
    *
    * 1.查出所有部门
    * 2.遍历所有部门
    *   如果是顶级部门,放入返回的集合中
    *   如果不是顶级部门,找到它的父部门,把自己放到父部门的children集合中*/
    /*@Override
    public List<Department> getTree() {

        List<Department> all = departmentMapper.loadAll();

        List<Department> result = new ArrayList<>();

        for (Department department : all) {
            if (department.getParent_id()==null){
                result.add(department);
            }else {//不是顶级部门,找到自己的父部门,把自己放入父部门的children集合中
                for (Department parent : all) {//寻找父部门
                    if (parent.getId()==department.getParent_id()){
                        List<Department> children = parent.getChildren();
                        children.add(department);
                        break;
                    }
                }
            }
        }
        return result;
    }*/
    @Override
    public List <Department> getTree(){
        List<Department> all = departmentMapper.loadAll();
        List<Department> result = new ArrayList<>();

        /*把部门集合转成一个map,map的key:部门id,map的value:部门对象
        * */
        Map<Long, Department> map = new HashMap<>();
        for (Department department : all) {
            map.put(department.getId(), department);
        }

        for (Department department : all) {
            if (department.getParent_id() == null){
                result.add(department);
            }else {
                Department parent = map.get(department.getId());
                List<Department> children = parent.getChildren();
                children.add(department);
            }
        }
        return result;
    }


}
